/**
 * mongoose操作MongoDB数据库
 */

const mongoose = require("mongoose")
// mongoose连接数据库，数据库名：bidding-system
mongoose.connect('mongodb://localhost/bidding-system')

let db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', () => {
  console.log("数据库连接成功")
});

// 定义schema，schema就相当于一张表的表头
let userSchema = new mongoose.Schema({
  username: {type: String, require: true},
  password: {type: String, require: true}
});

// 将schema 编译成一个 Model
let userModel = mongoose.model('users', userSchema);

// 用model来操作数据库
userModel.insertMany({
  username: "张依依", password: "123456"
}).then(msg => {
  console.log("插入数据成功", msg)
}).catch(err => {
  console.log("插入数据失败", err)
})
